﻿using System;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;

namespace Utils
{
    public class AccelerometerHelper
    {
        private Accelerometer acc;
        public bool IsSupported
        {
            get
            {
                return Accelerometer.IsSupported;
            }
        }

        public void Start()
        {
            if (acc == null)
            {
                try
                {
                    acc = new Accelerometer();
                }
                catch (Exception)
                {

                    throw;
                }
            }
            acc.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
            acc.Start();
            
            
        }

        public double Get()
        {

            var r = acc.CurrentValue.Acceleration;

            return Math.Asin(r.Z / r.Length()) / Math.PI * 180;
        }

        public void Stop()
        {
            acc.Stop();
            acc.Dispose();
            acc = null;
        }
    }
}
